package com.jy.rhin.application.infra;

import com.jy.framework.base.utils.Converter;
import com.jy.rhin.application.infra.event.MessageSaveEvent;
import com.jy.rhin.infra.repo.notfication.publish.query.PublishMessageQueryHandler;
import com.jy.rhin.infra.service.CacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description:
 * @Author: lww
 * @Date: 2022/11/16 16:56
 */
@Service
@Slf4j
public class MessageStatisticsHandler {

    @Resource
    CacheService cacheService;

    @Resource
    PublishMessageQueryHandler publishMessageQueryHandler;

    @TransactionalEventListener
    public void messageSaveEventHandler(MessageSaveEvent event) {
        String nowDate = Converter.toDateStr(new Date());
        Boolean hasKey = cacheService.hasKey(nowDate);
        if (Boolean.TRUE.equals(hasKey)) {
            log.debug("当天的消息通知数量+1");
            cacheService.addNowDateTotalCache(nowDate);
        } else {
            Integer nowDateTotal = publishMessageQueryHandler.queryNowDateTotal(nowDate);
            log.debug("查询当天的消息通知数量{}", nowDateTotal);
            cacheService.setNowDateTotalCache(nowDate, nowDateTotal);
        }
    }
}
